<?php

class Car_monitorApp extends BackendApp
{
	var $goodsnormal_mod;
	var $goodsnormalsub_mod;


	function __construct()
	{
		$this->Car_monitorApp();
	}

	function Car_monitorApp()
	{
		parent::BackendApp();
		$this->_goodsnormal_mod =& m('goodsnormal');
		$this->_goodsnormalsub_mod =& m('goodsnormalsub');

	}
	//整车列表
	function index()
	{

		try {
			$db =& db();
			/* $sql="SELECT  ifnull(a.a_vhc_code,'') as a_vhc_code,ifnull(a.a_username,'') as a_username,m.uuid
			 FROM zys_auth a
			 LEFT JOIN zys_member m ON m.user_id=a.a_userid
			 LEFT JOIN zys_authoperator r ON r.zao_usertid=m.user_id
			 left join zys_gpsnote gn on a.
			 WHERE r.zao_authed=1 AND r.zao_otid=".$this->visitor->get('unit');*/

			$sql = "SELECT  ifnull(a.a_vhc_code,'') as a_vhc_code,ifnull(a.a_username,'') as a_username,m.uuid,gn.gps_latitude as lat,
			 gn.gps_longitude as lng,gn.gps_gpstime as time,gn.gps_speed as speed,gn.gps_direction,d.name as railname,w.wm_code,gn.gps_inraildate,ifnull(gn.gps_railId,0) as gps_railId
			FROM zys_auth a
			left join zys_gpsnotes gn on a.a_vhc_code=gn.gps_vehicleno
			LEFT JOIN zys_member m ON m.user_id=a.a_userid
			left join zys_rail_define d on d.id=gn.gps_railId and d.status=1
			left join zys_waybill_main w on w.wm_tid=gn.gps_wmtid
			WHERE a.a_authed=1 AND a.a_type=1 order by gn.gps_latitude desc";

			$car_monitor = $db->getAll($sql);


			/*foreach ($car_monitor as $key=>$value) {
			 $url='http://121.42.190.106:8080/server/HbaseServlet?p={"op":"position","vhc":"'.$value['uuid'].'"}';
			 $ret=file_get_contents($url);
			 $arrret=json_decode($ret,true);
			 foreach ($arrret as $key2=>$value2) {
			 $car_monitor[$key]['lat'] =  $value2['lat'];
			 $car_monitor[$key]['lng'] =  $value2['lng'];
			 $car_monitor[$key]['speed'] =  $value2['speed'];
			 $car_monitor[$key]['time'] =  $value2['time'];
			 }
			 }*/


			if ($car_monitor) {

			}


			//	$this->assign('car_monitor', $car_monitor);
				$this->assign('car', ecm_json_encode($car_monitor));
				$this->display('car_monitor.index3.html');

		} catch (Exception $e) {
			$this->show_warning($e->getMessage());
		}

	}
	
	function msort(){
		
	}

	function calc($time1){
		$time2=date('y-m-d H:i:s',time());
		$time1 = strtotime($time1);
		$time2 = strtotime($time2);
		$time =$time2 -$time1;
		$day=intval($time/(3600*24));
		$hour=intval(($time%(3600*24))/3600);
		$min=intval(($time%(3600*24)%3600)/60);
		$sec=intval($time%(3600*24)%3600%60);
//		return $time;
		if($min==0)
		{
			return $sec.'秒';
		}
		elseif ($hour==0)
		{
			return $min.'分'.$sec.'秒';
		}
		elseif ($day==0)
		{
			return $hour.'小时'.$min.'分'.$sec.'秒';
		}


		else
		{
			return $day.'天'.$hour.'小时'.$min.'分'.$sec.'秒';
		}

	}
	function calc2($time1){
		$time2=date('y-m-d H:i:s',time());
		$time1 = strtotime($time1);
		$time2 = strtotime($time2);
		$time =$time2 -$time1;
		$day=intval($time/(3600*24));

		$hour=intval(($time%(3600*24))/3600);
		$min=intval(($time%(3600*24)%3600)/60);
		$sec=intval($time%(3600*24)%3600%60);
		return ($day<1&&$hour<1&&$min<3) ;

	}
	function ajax_car()
	{
		$query = $_POST['newquery'];
		$sqlw = "1=1 and a.a_authed=1 AND a.a_type=1 ";
		if ($query != null && $query != "") {
			$sqlw .= " AND (a.a_vhc_code LIKE '%" . $query . "%' OR a.a_username LIKE '%" . $query . "%')";
		}

		$db =& db();
		$sql = "SELECT  ifnull(a.a_vhc_code,'') as a_vhc_code,ifnull(a.a_username,'') as a_username,m.uuid,ifnull(gn.gps_latitude,'') as lat,
		 ifnull(gn.gps_longitude,'') as lng,gn.gps_gpstime as time,gn.gps_speed as speed,gn.gps_direction,w.wm_code,w.wm_awb_code,gn.gps_inraildate,ifnull(gn.gps_railId,0) as gps_railId,ifnull(gn.gps_wmtid,0) as gps_wmtid
		 FROM zys_auth a
		left join zys_gpsnotes gn on a.a_vhc_code=gn.gps_vehicleno
		LEFT JOIN zys_member m ON m.user_id=a.a_userid
		left join zys_waybill_main w on w.wm_tid=gn.gps_wmtid
		WHERE $sqlw ";

		$car_monitor = $db->getAll($sql);

		//echo ecm_json_encode($car_monitor);


		//TODO: 增加电子围栏定义
		$sql = "select id,name,lat,lng,offset from zys_rail_define where status=1
				  union all select 0 as id, '其它位置' as name, 0 as lat, 0 as lng, 0 as offset  ";
		$rail_define = $db->getAll($sql);

		if ($car_monitor && $rail_define) {
			$list = array();

			foreach ($rail_define as $k => $v) {
					$v["car"] = array();
				foreach ($car_monitor as $kCar => $vCar) {
					$vCar['speed']=intval($vCar['speed']);
				//	var_dump($vCar["gps_railId"] == $v["id"]);
					if ($vCar["gps_railId"] == $v["id"]) {
						$vCar['status']=$this->calc2($vCar[gps_inraildate]);
						$vCar[gps_inraildate]=$this->calc($vCar[gps_inraildate]);
						$v["car"][] = $vCar;
					}
				}
				$list[] = $v;
			}
			echo ecm_json_encode($list);
		}
	}

}